<template>
  <div class="app-container">
 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:finance:add']"
        >新增</el-button>
      </el-col>
     
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house_sale:finance:remove']"
        >删除</el-button>
      </el-col>
     
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="financeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="financeid" />
      <el-table-column label="购买房产编号" align="center" prop="buyid" /> -->
      <el-table-column label="客户名称" align="center" prop="custname" />
      <el-table-column label="房产名称" align="center" prop="housename" />

      <el-table-column label="票据本" align="center" prop="ticketname" />
      <el-table-column label="票据本编号" align="center" prop="ticketflow" />
      <el-table-column label="收款方式" align="center" prop="paidtypeid" />
      <el-table-column label="收款类别" align="center" prop="paidsortid" />
      <el-table-column label="收付类别" align="center" prop="oprtype" />
      <el-table-column label="收款金额" align="center" prop="paidmoney" />
      <el-table-column label="交款人" align="center" prop="paidperson" />
      <el-table-column label="公司入账银行" align="center" prop="inbank" />
      <el-table-column label="公司入账账号" align="center" prop="bankaccount" />
      <el-table-column label="出账银行" align="center" prop="outbank" />
      <!-- <el-table-column label="相关凭证号" align="center" prop="warrant" /> -->
      <el-table-column label="交款时间" align="center" prop="paidtime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paidtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到账日期" align="center" prop="paidintime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paidintime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
       <el-table-column label="操作时间" align="center" prop="oprtime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.oprtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    
      <el-table-column label="操作人" align="center" prop="userid" />
      <el-table-column label="是否有效" align="center" prop="invalid" />
     
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button  v-if="scope.row.invalid === '有效'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdate2(scope.row)"
          >作废</el-button>
           <el-button  v-if="scope.row.invalid === '作废'"
            size="mini"
            type="text"
            icon="el-icon-delete"
          >已作废</el-button>
        </template>
         <!-- v-hasPermi="['house_sale:finance:remove']" -->
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收款管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="购买房产编号" prop="buyid" hidden>
          <el-input v-model="form.buyid"  />
        </el-form-item>
        <el-form-item label="交款客户" prop="custname">
          <el-input v-model="form.custname"  disabled/>
        </el-form-item>
        <el-form-item label="房产名称" prop="housename">
          <el-input v-model="form.housename"  disabled/>
        </el-form-item>
        <el-form-item label="购房总款" prop="totalmoney">
          <el-input v-model="form.totalmoney"  disabled/>
        </el-form-item>
        <el-form-item label="已付金额" prop="yfmoney">
          <el-input v-model="form.yfmoney"  disabled/>
        </el-form-item>

        <el-form-item label="票据本" prop="ticketno">
          <el-select v-model="form.ticketno"  placeholder="请选择票据本" @change="getTicket" :style="{width:'100%'}"> 
                 <el-option
                  v-for="item in ticketList"
                  :key="item.ticketid"
                  :label="item.ticketno"
                  :value="item.ticketid"
                  :disabled="false"
                ></el-option> 
            </el-select>  
        </el-form-item>
        <el-form-item label="票据本编号" prop="ticketflow">
          <el-input v-model="form.ticketflow" disabled/>
        </el-form-item>
        <el-form-item label="收款方式" prop="paidtypeid">
            <el-select v-model="form.paidtypeid" @change="getpaidtype1" :style="{width:'100%'}">
                 <el-option
                  v-for="item in paidtypeList"
                  :key="item.paidtypeid"
                  :label="item.paidtypename+'-'+item.iscash"
                  :value="item.paidtypename"
                  :disabled="false"
                ></el-option> 
            </el-select>  
        </el-form-item>
        <el-form-item label="收款类别" prop="paidsortid">
          <el-select v-model="form.paidsortid"  @change="getpaidsort" :style="{width:'100%'}">
                 <el-option
                  v-for="item in paidsortList"
                  :key="item.paidsortid"
                  :label="item.paidsortname+'-'+item.paidtype"
                  :value="item.paidsortname"
                  :disabled="false"
                ></el-option> 
            </el-select>  
        </el-form-item>
         <el-form-item label="收付类别" prop="oprtype" hidden>
          <el-input v-model="form.oprtype" />
        </el-form-item>
        <el-form-item label="收款金额" prop="paidmoney">
          <el-input v-model="form.paidmoney" placeholder="请输入收款金额" @blur="getpaidsort1"/>
        </el-form-item>
        <el-form-item label="交款人" prop="paidperson">
          <el-input v-model="form.paidperson" placeholder="请输入交款人" />
        </el-form-item>
      
        <el-form-item label="入账账号" prop="inbank">
           <el-select v-model="form.inbank"  placeholder="---请选择入账账号---" @change="getinbank" :style="{width:'100%'}">
                 <el-option
                  v-for="item in inbankList"
                  :key="item.bankname"
                  :label="item.bankname+'-'+item.bankaccount"
                  :value="item.bankname"
                  :disabled="trueorfalse"
                ></el-option> 
            </el-select>  
        </el-form-item>
        <el-form-item label="公司入账账号" prop="bankaccount" hidden>
          <el-input v-model="form.bankaccount" />
        </el-form-item>
        <el-form-item label="出账银行" prop="outbank">
           <el-select v-model="form.outbank"  placeholder="---请选择出账银行---" :style="{width:'100%'}">
                 <el-option
                  v-for="item in outbankList"
                  :key="item.bankid"
                  :label="item.bankname"
                  :value="item.bankname"
                  :disabled="trueorfalse"
                ></el-option> 
            </el-select>  
          <!-- <el-input v-model="form.outbank" placeholder="请输入出账银行" /> -->
        </el-form-item>
        <el-form-item label="相关凭证号" prop="warrant">
          <el-input v-model="form.warrant" placeholder="请输入相关凭证号" />
        </el-form-item>
        <el-form-item label="交款时间" prop="paidtime">
          <el-date-picker clearable :style="{width:'100%'}"
            v-model="form.paidtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择交款时间"
            @change="getTime1"
            >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到账日期" prop="paidintime">
          <el-date-picker clearable :style="{width:'100%'}"
            v-model="form.paidintime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到账日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否有效" prop="invalid" hidden>
          <el-input v-model="form.invalid" placeholder="请输入是否有效" />
        </el-form-item>
        <el-form-item label="操作人" prop="userid" hidden>
          <el-input v-model="form.userid" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="操作时间" prop="oprtime" hidden>
          <el-date-picker clearable
            v-model="form.oprtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择录入时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFinance,getPaidType, beInvalid,getFinance, delFinance, addFinance, updateFinance,selectyfMoney } from "@/api/house_sale/finance";
import { updIsBalance }from "@/api/house_sale/houseinfo";
import { updNextNo}from "@/api/house_sale/ticket";
export default {
  name: "Finance",
  data() {
    return {
     
      trueorfalse: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收款管理表格数据
      financeList: [],
      paidtypeList: [],
      ticketList: [],
      paidsortList: [],
      outbankList: [],
      inbankList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buyid: null,
        ticketno: null,
        ticketflow: null,
        paidtypeid: null,
        paidmoney: null,
        paidperson: null,
        paidsortid: null,
        inbank: null,
        bankaccount: null,
        outbank: null,
        warrant: null,
        paidtime: null,
        paidintime: null,
        invalid: null,
        userid: null,
        oprtime: null,
        oprtype: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buyid: [
          { required: true, message: "购买房产编号不能为空", trigger: "blur" }
        ],
        ticketno: [
          { required: true, message: "票据本不能为空", trigger: "blur" }
        ],
        ticketflow: [
          { required: true, message: "票据本编号不能为空", trigger: "blur" }
        ],
        paidtypeid: [
          { required: true, message: "收款方式类别(关联付款方式表）不能为空", trigger: "blur" }
        ],
        paidmoney: [
          { required: true, message: "收款金额不能为空", trigger: "blur" }
        ],
        paidsortid: [
          { required: true, message: "收款类别不能为空", trigger: "blur" }
        ],
        paidtime: [
          { required: true, message: "交款时间不能为空", trigger: "blur" }
        ],
        invalid: [
          { required: true, message: "是否有效不能为空", trigger: "blur" }
        ],
        userid: [
          { required: true, message: "操作人不能为空", trigger: "blur" }
        ],
        oprtime: [
          { required: true, message: "录入时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.isbalance();
  },
  methods: {
    /**判断是否已结清 */
    isbalance(){
      if(this.form.yfmoney == this.form.totalmoney){
        updIsBalance(this.$route.query.hourseid);
      }
    },
  
     /**自动获取入账账号 */
    getinbank(temp){
      this.inbankList.forEach(element => {
        if(element.bankname === temp){

          this.form.bankaccount =element.bankaccount;
        }
      });
    },
     /**自动获取收款方式 */
    getpaidtype1(temp){
      this.paidtypeList.forEach(element => {
        if(element.paidtypename === temp && element.iscash ==='银行'){

          this.trueorfalse=false;
        }
        // element.paidtypename === temp && element.paidtypename !='银行放款'
        if(element.paidtypename === temp && element.iscash !='银行'){

          this.trueorfalse=true;
        }
      });
     
    },
     /**输入金额时自动获取收付款类别 */
    getpaidsort1(){
      
        if(this.form.oprtype ==='付款'){

          this.form.paidmoney= this.form.paidmoney-2*( this.form.paidmoney);
        }
        if(this.form.oprtype !='付款'){

          this.form.paidmoney=this.form.paidmoney;
        }
    },
    /**自动获取收付款类别 */
    getpaidsort(temp){
       this.paidsortList.forEach(element => {
        if(element.paidsortname === temp){

          this.form.oprtype =element.paidtype;
        }
      
      });
    },
     /**自动获取票据 */
    getTicket(temp){
       this.ticketList.forEach(element => {
        if(element.ticketid === temp){
          
          if(element.nextno<10){
            this.form.ticketflow=element.prefix+"00"+element.nextno;
          }else if(10<=element.nextno<100){
            this.form.ticketflow=element.prefix+"0"+element.nextno;
          }else{
            this.form.ticketflow=element.prefix+element.nextno;
          }

        }
      });
    },
  
    /**自动获取录入时间 */
    getTime1(){
      if(this.form.paidtime!=null){
        this.form.oprtime= this.form.paidtime;
      }
    },

    /** 查询收款管理列表 */
    getList() {
      this.loading = true;
      const id=this.$route.params.buyid;

      listFinance(id).then(response => {
        this.financeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        financeid: null,
        buyid: this.$route.params.buyid,
        custname: this.$route.query.custname,
        housename: this.$route.query.housename,
        ticketno: 1,
        ticketflow: null,
        paidtypeid: "现金",
        paidmoney: null,
        paidperson: this.$route.query.custname,
        paidsortid: "诚意金",
        inbank: null,
        bankaccount: null,
        outbank: null,
        warrant: null,
        paidtime: null,
        paidintime: null,
        invalid: "有效",
        userid: this.$store.state.user.name,
        oprtime: null,
        oprtype: "收款",
        totalmoney:this.$route.query.totalmoney,
        yfmoney:"0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.financeid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
    //  this.yfmoney
    if(this.form.yfmoney!='0'){
      selectyfMoney(this.$route.params.buyid).then(response => {
        if( response.yfmoney!=null){
          this.form.yfmoney = response.yfmoney;
        }
       
      });
     }
       getPaidType().then(response => {
         
        this.paidtypeList = response.paidtypeList;
        this.ticketList = response.ticketList;
        this.paidsortList =response.paidsortList;
        this.outbankList = response.outbankList;
        this.inbankList = response.inbankList;
        this.open = true;
        this.title = "添加收款管理";
      });
      
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const financeid = row.financeid || this.ids
      getFinance(financeid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收款管理";
      });
    },
     /**作废按钮操作 */
    handleUpdate2(row){
      const financeid = row.financeid 
      getFinance(financeid).then(response => {
        var form2 = response.data;
        form2.invalid="作废";

        this.$modal.confirm('是否确认作废').then(function() {
         return beInvalid(form2);//去作废
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("已作废");
      }).catch(() => {});
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.financeid != null) {
            updateFinance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
           updNextNo(this.form.ticketno).then(response => {
            // this.$modal.msgSuccess("修改成功");
          
            });
            addFinance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const financeids = row.financeid || this.ids;
      this.$modal.confirm('是否确认删除收款管理编号为"' + financeids + '"的数据项？').then(function() {
        return delFinance(financeids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/finance/export', {
        ...this.queryParams
      }, `finance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
